#!/bin/bash
# $Id: admin.funcs,v 1.2 2001/01/15 22:20:11 tom Exp $
#
#  ComeOn Point Functions! v0.9.2
#  - usate da vari altri moduli ComeOn Point...
#
#  AUTHOR: Beppe (beppe.dem@nsm.it)
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

# Args: source_file, domain, nldir
install_nodelist() {

  NODELIST="$1"
  FOR_DOMAIN="$2"
  NODEDIR="$3"

  vialog --backtitle "$TITLE" --infobox "Installo la nodelist '$NODELIST' per il domain $FOR_DOMAIN, attendere prego..." 0 0

  cp "$NODELIST" $NODEDIR/$FOR_DOMAIN.ndl
  chown fnet.uucp $NODEDIR/$FOR_DOMAIN.ndl

  echo "y" | su -l fnet -c "ifindex" >/dev/null
}

# Args: source, dest
add_alias() {

  SOURCE=$1
  DEST=$2
  ALIASES=/etc/aliases
  ENTRY="`grep $SOURCE: $ALIASES`"

  vialog --backtitle "$TITLE" --infobox "Creo un alias per redirigere le mail per $SOURCE a $DEST ..." 0 0

  if [ ! "$ENTRY" ]
  then
    echo "$SOURCE: $DEST" >>$ALIASES
  else
    OLDDEST="`echo $ENTRY | cut -f2 -d:`"
    mkstemp alias
    sed /"$SOURCE:"/s/"$OLDDEST"/" $DEST"/ $ALIASES >$tmp_alias
    cat $tmp_alias >$ALIASES
    rm $tmp_alias
  fi
  mkaliases >/dev/null
} 

# Args: Areas_pathname, newsgroup, area_ftn, description [, expire_days]
# Returns: 1 - innd not running
#          2 - already existing newsgroup
#          3 - already existing ftn_area
create_newsgroup() {

  AREAS="$1"
  NEWSGROUP="$2"
  AREA="$3"
  DESCRIPTION="$4"
  EXPIRE_DAYS="$5"

  NEWSGROUPS=~news/newsgroups
  EXPIRECTL=~news/expire.ctl

  vialog --backtitle "$TITLE" --infobox "Creo il newsgroup $NEWSGROUP per contenere i messaggi dell'area $AREA..." 0 0

  if [ ! "`ps ax | grep ~news/etc/innd`" ]
  then
    return 1
  fi

  if [ "`cut $NEWSGROUPS -f1 | cut -f1 -d\  | grep -x $NEWSGROUP`" ]
  then
    return 2
  fi

  if [ "`cut $AREAS -f1 | cut -f1 -d\  | grep -x $AREA`" ]
  then
    return 3
  fi

  su -l news -c "bin/ctlinnd newgroup $NEWSGROUP" >/dev/null
  echo -e "$NEWSGROUP\t$DESCRIPTION" >> $NEWSGROUPS
  echo -e "$AREA\t$NEWSGROUP\t`echo $NEWSGROUP | cut -f1 -d.`" >> $AREAS

  if [ "$EXPIRE_DAYS" ]; then
    echo "$NEWSGROUP:A:2:$EXPIRE_DAYS:$EXPIRE_DAYS" >> $EXPIRECTL
  fi

  chown news.news $NEWSGROUPS $EXPIRECTL
  chmod 440 $EXPIRECTL
  chown fnet.uucp $AREAS
}

# Returns: 0 if ok, 1 if (innd not running), 2 if (no that newsgroup)
# Args: Areas_pathname, newsgroup
rmgroup() {

  AREAS=$1
  NEWSGR=$2
  NEWSGROUPS=~news/newsgroups
  EXPIRE=~news/expire.ctl

  vialog --backtitle "$TITLE" --infobox "Elimino il newsgroup $NEWSGR ed i messaggi in esso contenuti, attendere prego. Questa operazione potrebbe durare alcuni minuti." 0 0

  if [ ! "`ps x | grep ~news/etc/innd`" ]
  then
    return 1
  fi

  if [ ! "`cut -f1 $NEWSGROUPS | cut -f1 -d\  | grep -x $NEWSGR`" ]
  then
    return 2
  else

# Updating newsgroups file
    if [ -f $NEWSGROUPS ]
    then
      mkstemp groups
      mv $NEWSGROUPS $NEWSGROUPS~
      grep -v $NEWSGR $NEWSGROUPS~ >$tmp_groups
      mv $tmp_groups $NEWSGROUPS
    else
      :> $NEWSGROUPS
    fi
    chown news.news $NEWSGROUPS

# Updating Areas file
    if [ -f $AREAS ]
    then
      mkstemp areas
      mv $AREAS $AREAS~
      grep -v $NEWSGR $AREAS~ >$tmp_areas
      mv $tmp_areas $AREAS
    else
      :> $AREAS
    fi
    chown fnet.uucp $AREAS

# Updating expire.ctl file
    if [ -f $EXPIRE ]
    then
      mkstemp expire
      mv $EXPIRE $EXPIRE~
      grep -v "$NEWSGR:" $EXPIRE~ >$tmp_expire
      mv $tmp_expire $EXPIRE
    else
      :> $EXPIRE
    fi
    chown news.news $EXPIRE
    chmod 444 $EXPIRE
# Remove pending articles
    echo -e "\n$NEWSGR:A:0:0:0" >>$EXPIRE
    su -l news -c "bin/news.daily" >/dev/null
    mkstemp expire
    grep -v $NEWSGR $EXPIRE >$tmp_expire
    mv $tmp_expire $EXPIRE
    chown news.news $EXPIRE
    chmod 444 $EXPIRE

# Updating active file
    su -l news -c "bin/ctlinnd rmgroup $NEWSGR" >/dev/null
  fi
}

# Args: newsgroups_wildmat, days_to_keep
set_expire_time() {

	NEWSGROUP="$1"
	KEEPDAYS="$2"
	EXPIRE=~news/expire.ctl

        vialog --backtitle "$TITLE" --infobox "Imposto a $KEEPDAYS il numero di giorni di mantenimento dei messaggi del newsgroup $NEWSGROUP..." 0 0

	if [ ! -f $EXPIRE ]
	then
		:> $EXPIRE

	elif [ "`cut $EXPIRE -s -f1 -d: | grep -x $NEWSGROUP`" ]
	then
		mkstemp expire
		grep -v "$NEWSGROUP:" $EXPIRE >$tmp_expire
		mv $tmp_expire $EXPIRE
	fi
	echo -e "\n$NEWSGROUP:A:2:$KEEPDAYS:$KEEPDAYS" >>$EXPIRE
	chown news.news $EXPIRE
	chmod 444 $EXPIRE
}
